package net.huashitong.ssydt.provider.money.service;

import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.exceptions.ParamErrorServiceException;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.money.constant.OperationEnum;
import net.huashitong.ssydt.money.entity.SsMoneyGold;
import net.huashitong.ssydt.money.entity.SsMoneyGoldRecord;
import net.huashitong.ssydt.money.entity.vo.SsMoneyGoldQueryInVO;
import net.huashitong.ssydt.money.entity.vo.SsMoneyGoldRecordInVO;
import net.huashitong.ssydt.provider.money.mapper.SsMoneyGoldMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huiyang.yu
 */
@Service
@Transactional
public class SsMoneyGoldServiceImpl implements SsMoneyGoldService {

    private SsMoneyGoldMapper mapper;

    private SsMoneyGoldRecordService ssMoneyGoldRecordService;


    @Autowired
    public SsMoneyGoldServiceImpl(SsMoneyGoldMapper mapper, SsMoneyGoldRecordService ssMoneyGoldRecordService) {
        this.mapper = mapper;
        this.ssMoneyGoldRecordService = ssMoneyGoldRecordService;
    }

    @Override
    public SsMoneyGold getByUserId(String userId) {
        return mapper.getByUserId(userId);
    }

    @Override
    public void save(SsMoneyGold ssMoneyGold) {
        mapper.save(ssMoneyGold);
    }

    @Override
    public void updateGoldNumberById(SsMoneyGold ssMoneyGold) {
        mapper.updateGoldNumberById(ssMoneyGold);
    }

    @Override
    public void updateGoldNumber(SsMoneyGoldRecordInVO ssMoneyGoldRecordInVO) {
        SsMoneyGold ssMoneyGold = getByUserId(ssMoneyGoldRecordInVO.getUserId());
        if (ssMoneyGold == null) {
            throw new ServiceException("用户的账户不存在");
        }

        // 金币增(+)减(-)
        long recordGoldNumber = ssMoneyGoldRecordInVO.getGoldNumber();
        switch (ssMoneyGoldRecordInVO.getOperation()) {
            case "+":
                break;
            case "-":
                recordGoldNumber = -recordGoldNumber;
                break;
            default:
                throw new ParamErrorServiceException("金币操作类型不正确");
        }

        // 金币增(+)减(-)
        long goldNumber = ssMoneyGold.getGoldNumber() + recordGoldNumber;
        if (goldNumber < 0) {
            goldNumber = 0;
        }
        ssMoneyGold.setGoldNumber(goldNumber);
        updateGoldNumberById(ssMoneyGold);

        // 记录金币操作记录
        SsMoneyGoldRecord ssMoneyGoldRecord = new SsMoneyGoldRecord();
        ssMoneyGoldRecord.setUserId(ssMoneyGoldRecordInVO.getUserId());
        ssMoneyGoldRecord.setAccountId(ssMoneyGold.getId());
        ssMoneyGoldRecord.setOperation(ssMoneyGoldRecordInVO.getOperation());
        ssMoneyGoldRecord.setGoldNumber(ssMoneyGoldRecordInVO.getGoldNumber());
        ssMoneyGoldRecord.setRemainderMoney(ssMoneyGold.getGoldNumber());
        ssMoneyGoldRecord.setDescription(ssMoneyGoldRecordInVO.getDescription());
        ssMoneyGoldRecord.setOrderId("");
        ssMoneyGoldRecordService.save(ssMoneyGoldRecord);
    }

    @Override
    public PaginationResult<SsMoneyGold> paginationQuery(SsMoneyGoldQueryInVO ssMoneyGoldQueryInVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(ssMoneyGoldQueryInVO.getPageNumber());
        result.setPageSize(ssMoneyGoldQueryInVO.getPageSize());

        // 查询记录总数
        int total = mapper.queryCount(ssMoneyGoldQueryInVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsMoneyGold> list = mapper.query(ssMoneyGoldQueryInVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public void modifyAndRecordBill(String userId, OperationEnum operationEnum, long modifyGoldNumber, String description) {
        SsMoneyGold ssMoneyGold = this.getByUserId(userId);
        if (OperationEnum.ADD == operationEnum) {
            ssMoneyGold.setGoldNumber(ssMoneyGold.getGoldNumber() + modifyGoldNumber);
        } else {
            if (modifyGoldNumber > ssMoneyGold.getGoldNumber()) {
                throw new ServiceException("金币不足");
            }
            ssMoneyGold.setGoldNumber(ssMoneyGold.getGoldNumber() - modifyGoldNumber);
        }
        this.updateGoldNumberById(ssMoneyGold);

        SsMoneyGoldRecord ssMoneyGoldRecord = new SsMoneyGoldRecord();
        ssMoneyGoldRecord.setUserId(userId);
        ssMoneyGoldRecord.setAccountId(ssMoneyGold.getId());
        if (OperationEnum.ADD == operationEnum) {
            ssMoneyGoldRecord.setOperation("+");
        } else {
            ssMoneyGoldRecord.setOperation("-");
        }
        ssMoneyGoldRecord.setGoldNumber(modifyGoldNumber);
        ssMoneyGoldRecord.setRemainderMoney(ssMoneyGold.getGoldNumber());
        ssMoneyGoldRecord.setDescription(description);
        ssMoneyGoldRecord.setOrderId("");
        ssMoneyGoldRecordService.save(ssMoneyGoldRecord);
    }
}
